<template>
  <div id="app" @click="stopView">
    <router-view />
  </div>
</template>
<script>
import { handTheme, themeData } from "@/utils/publicStyle.js";
import { getQueryVariable } from "@/utils/common.js";
import router from "@/router";
import CONFIG from "@/utils/config.js";

export default {
  data() {
    return {
      routeIndex: 0,
      routeTask: 0,
      taskTimer: 5 * 60, // 页面切换间隔 秒
    };
  },
  computed: {},
  created() {
    this.setRemUnit();
    // this.getCashData();
  },
  mounted() {
    handTheme(this.$store.state.theme); // 初始化主题样式
    // console.log(this.getSize(12));
    this.initAutoSwitchPage();

    // this.getDate();
  },
  methods: {
    // 获取缓存数据
    getCashData() {
      // console.log(CONFIG.prodUrl.replace("just_search", "init_cache"));
      let _this = this;
      this.ajax({
        sql: "",
        config: {
          url: CONFIG.prodUrl.replace("just_search", "init_cache"),
        },
      }).then((res) => {
        // console.log(JSON.stringify(res));
        this.$store.commit("setCashData", res);
      });

      // let xhr = new XMLHttpRequest();
      // xhr.open("POST", CONFIG.prodUrl.replace("just_search", "init_cache"));
      // xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
      // xhr.responseType = "json";
      // xhr.onreadystatechange = function () {
      //   if (xhr.readyState == 4) {
      //     if (xhr.status == 200) {
      //       console.log(xhr.response);
      //     } else {
      //       console.error(xhr.statusText);
      //     }
      //   }
      // };

      // xhr.send(`{"clean": true}`);
    },
    // 获取服务器时间
    getDate() {
      this.ajax({
        sql: ` SELECT now()`,
      }).then((res) => {
        if (res && res.length) {
          this.$store.commit("setSysDate", new Date(res[0].NOW));
        }
      });
    },
    setRemUnit() {
      if (navigator.userAgent.indexOf("iPad") > -1) {
        this.$store.commit("setRemUnit", 12);
      } else if (document.body.clientWidth < 1500) {
        // PC 且宽度小于1500时
        this.$store.commit("setRemUnit", 12);
      } else {
        this.$store.commit("setRemUnit", 16);
      }
    },
    // 自动切换页面显示
    initAutoSwitchPage() {
      let viewType = getQueryVariable("vt");
      if (viewType) {
        this.getTypeRoute(viewType);
      }
    },
    // 获取当前类型的页面路由
    getTypeRoute(viewType) {
      let rt = router.options.routes[1].children.filter((item) => {
        return item.viewType == viewType;
      });
      // console.log(rt);
      this.setTask(rt);
    },
    setTask(rt) {
      clearInterval(this.routeTask);

      this.routeTask = setInterval(() => {
        this.routeIndex++;
        if (this.routeIndex >= rt.length) {
          this.routeIndex = 0;
        }

        this.$router.push({
          path: rt[this.routeIndex].path,
          query: this.$route.query,
        });
      }, this.taskTimer * 1000);
    },
    // 停止轮播
    stopView() {
      clearInterval(this.routeTask);
    },
  },
};
</script>

<style lang="scss">
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  font-size: 14px;
  overflow: hidden;
}
</style>
